#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;

     bool searchHelper(int A[], int lo, int hi, int target) {
                if (lo > hi) return false;
                int mid = lo + (hi-lo)/2;
                if (A[mid] == target) return true;
                if (A[lo] == A[mid] || A[mid] == A[hi]) {
                    return searchHelper(A, lo, mid-1, target) | searchHelper(A, mid+1, hi, target);
                } else if (A[lo] <  A[mid] ) {  /* lower half is sorted */
                    if (target >= A[lo] && target < A[mid])
                       return searchHelper(A, lo, mid-1, target); 
                    else 
                        return searchHelper(A, mid+1, hi, target);
                } else if (A[mid] < A[hi]) { /* higher half is sorted */
                    if (target > A[mid] && target <= A[hi])
                        return searchHelper(A, mid+1, hi, target);
                    else 
                       return searchHelper(A, lo, mid-1, target); 
                }
        }

class Solution {
    public:
        bool search(int A[], int n, int target) {
            int lo = 0, hi = n-1;
            return searchHelper(A, 0, n-1, target);                        
        }
};

int main(int argc, char **argv)
{
    Solution mySolution;
    int A[] = {1,1,1,1,3,1,1};

    cout << "-----------------Test 1--------------------" << endl;
    cout << "Search 2" << mySolution.search(A, sizeof(A)/sizeof(int), 2) << endl;
    cout << "Search 1" << mySolution.search(A, sizeof(A)/sizeof(int), 1) << endl;
    cout << "Search 3" << mySolution.search(A, sizeof(A)/sizeof(int), 3) << endl;

    cout << "-----------------Test 2--------------------" << endl;


    cout << "-----------------Test 3--------------------" << endl;


    cout << "-----------------Test 4--------------------" << endl;


    cout << "-----------------Test 5--------------------" << endl;



}
